package com.zlict.commonelasticsearch.controller;

import com.zlict.commonelasticsearch.service.ElasticsearchAuditService;
import com.zlict.commonelasticsearch.utils.AuditLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 * 
 * @author zlict
 */
@RestController
@RequestMapping("/api/elasticsearch/test")
public class ElasticsearchTestController {
    
    @Autowired
    private ElasticsearchAuditService elasticsearchAuditService;
    
    @Autowired
    private AuditLogUtils auditLogUtils;
    
    /**
     * 测试Elasticsearch连接
     */
    @GetMapping("/elasticsearch")
    public ResponseEntity<Map<String, Object>> testElasticsearch() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 检查连接状态
            boolean isAvailable = elasticsearchAuditService.isElasticsearchAvailable();
            result.put("available", isAvailable);
            
            if (isAvailable) {
                // 获取集群信息
                String clusterInfo = elasticsearchAuditService.getClusterInfo();
                result.put("clusterInfo", clusterInfo);
                result.put("message", "Elasticsearch连接正常");
            } else {
                result.put("message", "Elasticsearch连接失败");
            }
            
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "测试失败: " + e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 测试审计日志功能
     */
    @PostMapping("/audit")
    public ResponseEntity<Map<String, Object>> testAuditLog() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 记录测试审计日志
            auditLogUtils.recordAuditLog("测试操作", "成功", "这是一条测试审计日志");
            
            // 记录载体操作日志
            auditLogUtils.recordCarrierOperation("载体入库", "载体ID: 12345", "成功");
            
            // 记录登录日志
            auditLogUtils.recordLoginAuditLog("testuser", "登录", "成功", "用户登录系统");
            
            result.put("success", true);
            result.put("message", "审计日志测试完成");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "审计日志测试失败: " + e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 测试查询审计日志
     */
    @GetMapping("/audit/query")
    public ResponseEntity<Map<String, Object>> testQueryAuditLogs() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 查询最近7天的审计日志
            String logs = elasticsearchAuditService.queryRecentAuditLogs(7);
            result.put("logs", logs);
            result.put("success", true);
            result.put("message", "查询审计日志成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询审计日志失败: " + e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 测试直接查询ES索引
     */
    @GetMapping("/audit/direct")
    public ResponseEntity<Map<String, Object>> testDirectQuery() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 直接查询ES索引
            String directResult = elasticsearchAuditService.queryAllAuditLogs();
            result.put("data", directResult);
            result.put("success", true);
            result.put("message", "直接查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "直接查询失败: " + e.getMessage());
        }
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> healthCheck() {
        Map<String, Object> result = new HashMap<>();
        result.put("status", "UP");
        result.put("application", "common-elasticsearch");
        result.put("version", "1.0.0");
        return ResponseEntity.ok(result);
    }
}

